package com.larson.creator.view
{
	import com.larson.creator.ApplicationFacade;
	import com.larson.creator.model.*;
	import com.larson.creator.model.VO.*;
	import com.larson.creator.model.vo.LineDataVO;
	import com.larson.creator.view.components.*;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.NumberFormatter;
		
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	public class crsMediator extends Mediator implements IMediator
	{
        // Cannonical name of the Mediator
        public static const NAME:String = "crsMediator";
        
		private var fileConvertProxy:FileConvertProxy;  
		      
 		private var slopeFormatter:NumberFormatter;		
		private var distanceFormatter:NumberFormatter;		
		
       /**
         * Constructor. 
         */
		public function crsMediator (viewComponent:crsScreen)
		{
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );
            slopeFormatter = new NumberFormatter();
            distanceFormatter = new NumberFormatter();
            slopeFormatter.precision = 2;
            slopeFormatter.useNegativeSign = true;
            slopeFormatter.useThousandsSeparator = true;
            distanceFormatter.precision = 3;
            distanceFormatter.useNegativeSign = true;
            distanceFormatter.useThousandsSeparator = true;
		}
		
		
        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
        	
            return [ 
					ApplicationFacade.CONVERT_FILE_COMPLETE
					//ApplicationFacade.LOADING_COMPLETE,
					//ApplicationFacade.LOAD_CONFIG_FAILED,
					//ApplicationFacade.LOAD_RESOURCE_FAILED
					];
					
			//return new Array();
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{

				case ApplicationFacade.CONVERT_FILE_COMPLETE:
					// Get the convert data Proxy in order to acquire the data provider for our data grid.
					fileConvertProxy = facade.retrieveProxy( FileConvertProxy.NAME ) as FileConvertProxy;
					var data:ArrayCollection = fileConvertProxy.correctedData;
					var idx:Number = 0;
					var courseText:String = "";
					for(idx = 0; idx < data.length; idx++ ) {
						var seg:LineDataVO = data[idx];
						var segText:String;
						segText = distanceFormatter.format(seg.x) + "\t\t" + slopeFormatter.format(seg.y) + "\t" + "0\n";
						courseText += segText;
					}
					
					crs.localData = courseText;
					
					break;
				/*					
				case ApplicationFacade.LOADING_COMPLETE:
					// all done
					// show the main screen
					this.sendNotification( ApplicationFacade.VIEW_MAIN_SCREEN );
					break;
					
				case ApplicationFacade.LOAD_CONFIG_FAILED:
				case ApplicationFacade.LOAD_RESOURCE_FAILED:
					// error reading the config XML fille
					// show the error
					this.splashScreen.errorText.text = note.getBody() as String;
					this.splashScreen.errorBox.visible = true;
					break;
				*/

            }
        }

        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return SplashScreen the viewComponent cast to org.puremvc.as3.demos.flex.appskeleton.view.components.SplashScreen
         */
		 
        protected function get crs():crsScreen
		{
            return viewComponent as crsScreen;
        }
		
	}
}